//
//  WYCCreditCardController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/12/25.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCCreditCardController.h"
#import "WYCBillModel.h"
#import "WYCRepayFramModel.h"
#import "WYCCreditCell.h"

@interface WYCCreditCardController ()
{
    NSString * bankName;
    NSString * bankid;
    NSString * cardid;
    
}
/** 列表数组*/
@property(nonatomic,strong)NSMutableArray * billArray;

/** 列表数组*/
@property(nonatomic,strong)NSMutableArray * bankidArray;

/** 列表数组*/
@property(nonatomic,strong)NSMutableArray * cardidArray;

/** 列表数组*/
@property(nonatomic,strong)NSMutableArray * banknameArray;

/** 取反 */
@property (nonatomic, strong) UIButton  * xuanzhongButton;

/** 显示View*/
@property(nonatomic,strong)UIView * showView;

/** 删除按钮*/
@property(nonatomic,strong) UIButton * buttonsender;

/** 遮罩*/
@property(nonatomic,strong)UIView * ejectView;
@end

@implementation WYCCreditCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.billArray = [[NSMutableArray alloc] init];
    self.bankidArray = [[NSMutableArray alloc] init];
    
    self.banknameArray = [[NSMutableArray alloc] init];
    self.cardidArray = [[NSMutableArray alloc] init];
    //    [self setupRuqst];
    
    [self setupTableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRuqst)];
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)setVlus:(NSString *)Str{
    
    Str = [NSString stringWithFormat:@"%lu",(unsigned long)self.billArray.count];
}
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//
//
//}

- (void)setupTableView
{
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = NavHeight;
    self.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    self.tableView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    self.tableView.sectionHeaderHeight = 220 * px;
    self.tableView.rowHeight = 150 * px;
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置滚动条的内边距
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (void)setupRuqst {
    [self.billArray removeAllObjects];
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{
                    WYCRepayFramModel * BillFrameModel = [[WYCRepayFramModel alloc] init];
                    BillFrameModel.Billmodel = model;
                    [self.billArray addObject:BillFrameModel];
                    [self.bankidArray addObject:model.lastNumber];
                    [self.banknameArray addObject:model.bankName];
                    [self.cardidArray addObject:model.cardId];
                }
                [self.delegate setVlus:[NSString stringWithFormat:@"%lu",(long)self.billArray.count]];
            }
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [self.tableView.mj_header endRefreshing];
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.billArray.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull WYCCreditCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    cell.layer.zPosition = indexPath.row;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WYCRepayFramModel * model = self.billArray[indexPath.row];
    
    return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WYCCreditCell * cell = [WYCCreditCell cellWithTableView:tableView];
    cell.billmodelFrame = self.billArray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
    
    //设定最小的长按时间 按不够这个时间不响应手势
    
    longPressGR.minimumPressDuration = 1;
    
    [cell addGestureRecognizer:longPressGR];
    
    return cell;
}

//实现手势对应的功能

-(void)lpGR:(UILongPressGestureRecognizer *)lpGR

{
    
    
    
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        CGPoint point = [lpGR locationInView:self.tableView];
        
        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
        
        if (indexPath != nil) {
            NSLog(@"----------%@", self.bankidArray[indexPath.row]);
            bankid = self.bankidArray[indexPath.row];
            
            bankName = self.banknameArray[indexPath.row];
            NSLog(@"------%@",bankid);
            
            cardid = self.self.cardidArray[indexPath.row];
        }
        self.ejectView = [[UIView alloc] init];
        self.ejectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.ejectView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [[UIApplication sharedApplication].keyWindow  addSubview:self.ejectView];
        
        UIView * showView = [[UIView alloc] init];
        showView.frame = CGRectMake(80 * px, ScreenHeight / 2 - 320 * px, ScreenWidth - 160 * px, 640 * px);
        showView.layer.cornerRadius = 3;
        showView.layer.masksToBounds = YES;
        showView.backgroundColor = [UIColor whiteColor];
        [self.ejectView addSubview:showView];
        self.showView = showView;
        
        UIView * topView = [[UIView alloc] init];
        topView.frame = CGRectMake(0, 0, showView.width, 150 * px);
        topView.backgroundColor = WYCGlobalBg;
        [showView addSubview:topView];
        
        UILabel * showlabel = [[UILabel alloc] init];
        showlabel.frame = CGRectMake(0, 0, topView.width, topView.height);
        showlabel.text = [NSString stringWithFormat:@"删除%@%@",bankName,bankid];
        showlabel.font = [UIFont boldSystemFontOfSize:BigFont];
        showlabel.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:showlabel];
        
        
        
        for (NSInteger i = 0; i < 2; i++) {
            UIButton * button = [[UIButton alloc] init];
            button.tag = i;
            [button setImage:[UIImage imageNamed:@"nomor"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"Select"] forState:UIControlStateDisabled];
            button.frame = CGRectMake(40 * px, 200 * px + (300 / 2 * i * px) , 80 * px, 80 * px);
            [button layoutIfNeeded];//强制布局
            [button addTarget:self action:@selector(deletebuttonclick:) forControlEvents:UIControlEventTouchUpInside];
            [showView addSubview:button];
            self.buttonsender = button;
            
            if (i == 0) {
                [self deletebuttonclick:button];
                
                button.enabled = NO;
                
                self.xuanzhongButton = button;
                
            }
            
            
            UILabel * onelabel = [[UILabel alloc] init];
            onelabel.frame = CGRectMake(CGRectGetMaxX(self.buttonsender.frame) + 20 * px, CGRectGetMaxY(showlabel.frame) + 20 * px, showView.width - 160 * px, 150 * px);
            onelabel.numberOfLines = 0;
            onelabel.textColor = [UIColor grayColor];
            onelabel.text = @"隐藏该卡，重新导不在首页显示，或设置管理卡片中恢复";
            onelabel.font = [UIFont systemFontOfSize:WyzFont];
            [topView addSubview:onelabel];
            
            
            UILabel * towlabel = [[UILabel alloc] init];
            towlabel.frame = CGRectMake(onelabel.x, CGRectGetMaxY(onelabel.frame), onelabel.width, onelabel.height);
            towlabel.numberOfLines = 0;
            towlabel.textColor = [UIColor grayColor];
            towlabel.text = @"删除该卡，如果恢复请重新导入网银，或删除已导入邮箱重新导入";
            towlabel.font = [UIFont systemFontOfSize:WyzFont];
            //                towlabel.textAlignment = NSTextAlignmentCenter;
            [topView addSubview:towlabel];
            
            
            UIButton * mainbutton = [[UIButton alloc] init];
            mainbutton.frame = CGRectMake(0, showView.height - 150 * px, showView.width / 2, 150 * px);
            [mainbutton.layer setBorderColor:[UIColor grayColor].CGColor];
            [mainbutton.layer setBorderWidth:0.5];
            [mainbutton.layer setMasksToBounds:YES];
            [mainbutton setTitle:@"取消" forState:UIControlStateNormal];
            [mainbutton setTitleColor:WYCmainColor forState:UIControlStateNormal];
            [mainbutton addTarget:self action:@selector(mainbuttonClick) forControlEvents:UIControlEventTouchUpInside];
            mainbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [showView addSubview:mainbutton];
            
            
            
            UIButton * returebutton = [[UIButton alloc] init];
            returebutton.frame = CGRectMake(CGRectGetMaxX(mainbutton.frame), mainbutton.y, showView.width / 2, 150 * px);
            [returebutton.layer setBorderColor:[UIColor grayColor].CGColor];
            [returebutton.layer setBorderWidth:0.5];
            [returebutton.layer setMasksToBounds:YES];
            [returebutton setTitle:@"确定" forState:UIControlStateNormal];
            [returebutton setTitleColor:WYCmainColor forState:UIControlStateNormal];
            [returebutton addTarget:self action:@selector(returebuttonClick) forControlEvents:UIControlEventTouchUpInside];
            returebutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [showView addSubview:returebutton];
            
        }
        
    }
    
    if (lpGR.state == UIGestureRecognizerStateEnded)//手势结束
        
    {
        
        
        
    }
    
}
- (void)deletebuttonclick:(UIButton *)sender{
    
    
    WYCLogFunc;
    //修改按钮状态
    self.xuanzhongButton.enabled = YES;
    sender.enabled = NO;
    self.xuanzhongButton = sender;
    WYCLog(@"---------%ld",(long)self.xuanzhongButton.tag);
}

- (void)mainbuttonClick{
    self.showView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
- (void)returebuttonClick{
    
    
    if(self.xuanzhongButton.tag == 0){
        
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"token"] = [WYCAccountTool unarchivetoken];
        params[@"cardId"] = cardid;
        params[@"isHideCard"] = @"1";
        WYCLog(@"-------%@",params);
        [HttpTool POST:creditcardsetHideCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [self showToast:@"成功"];
                [self mainbuttonClick];
                
                [self.tableView.mj_header beginRefreshing];
                
            }else{
                
                [self showToast:msgStr];
                
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
            [self showToast:Failed];
            
        }];
    }else{
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"token"] = [WYCAccountTool unarchivetoken];
        params[@"cardId"] = cardid;
        [HttpTool POST:creditcardunbindCreditCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [self showToast:@"成功"];
                [self mainbuttonClick];
                [self.tableView reloadData];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"yes" object:nil];
                
            }else{
                
                [self showToast:msgStr];
                
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
            [self showToast:Failed];
            
        }];
        
    }
}
@end


